package cn.laixueit.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;

@Service
public class GoodsHtmlService {

    @Autowired
    private GoodsWebService goodsWebService;

    @Autowired
    private TemplateEngine templateEngine;

    public void createHtml(Long spuId){
        // 封装数据模型
        Map<String, Object> dataMap = goodsWebService.loadData(spuId);

        // 装载上下文
        Context context = new Context();
        context.setVariables(dataMap);

        // 文件输出
        File file = new File("D:\\chuangmei2\\nginx-1.18.0 -chuangmei\\html",spuId+".html");
        PrintWriter writer = null;

        try {
            writer = new PrintWriter(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        templateEngine.process("item",context,writer);
    }

    public void deleteHtml(Long id) {
        File file = new File("D:\\chuangmei2\\nginx-1.18.0 -chuangmei\\html",id+".html");
        file.deleteOnExit();
    }
}
